// 数据字典测试工具
import { 
  getAllSupplierLevels,
  getAllSupplierCategories,
  getAllBusinessNatures,
  getAllIndustries,
  getAllLocations,
  getAllPersons
} from '@/Https/server'

// 测试所有数据字典API
export async function testAllDataDictionaries() {
  console.log('🔍 开始测试所有数据字典API...')
  
  const testResults = {
    success: true,
    results: {},
    summary: {}
  }

  const tests = [
    {
      name: '供应商等级',
      api: getAllSupplierLevels,
      key: 'levels'
    },
    {
      name: '供应商类别',
      api: getAllSupplierCategories,
      key: 'categories'
    },
    {
      name: '业务性质',
      api: getAllBusinessNatures,
      key: 'natures'
    },
    {
      name: '行业',
      api: getAllIndustries,
      key: 'industries'
    },
    {
      name: '地区',
      api: getAllLocations,
      key: 'locations'
    },
    {
      name: '人员',
      api: getAllPersons,
      key: 'persons'
    }
  ]

  for (const test of tests) {
    try {
      console.log(`📡 测试 ${test.name}...`)
      const response = await test.api()
      
      testResults.results[test.key] = {
        success: response.success,
        data: response.data,
        message: response.message,
        count: response.data ? response.data.length : 0
      }

      if (response.success) {
        console.log(`✅ ${test.name} - 成功 (${response.data ? response.data.length : 0} 条)`)
        if (response.data && response.data.length > 0) {
          console.log(`   示例数据:`, response.data[0])
        }
      } else {
        console.log(`❌ ${test.name} - 失败: ${response.message}`)
        testResults.success = false
      }
    } catch (error) {
      console.log(`❌ ${test.name} - 异常: ${error.message}`)
      testResults.results[test.key] = {
        success: false,
        error: error.message,
        count: 0
      }
      testResults.success = false
    }
  }

  // 生成摘要
  testResults.summary = {
    totalTests: tests.length,
    successCount: Object.values(testResults.results).filter(r => r.success).length,
    totalDataCount: Object.values(testResults.results).reduce((sum, r) => sum + (r.count || 0), 0)
  }

  console.log('📊 测试摘要:', testResults.summary)
  return testResults
}

// 测试单个数据字典
export async function testDataDictionary(name, api) {
  try {
    console.log(`📡 测试 ${name}...`)
    const response = await api()
    
    if (response.success) {
      console.log(`✅ ${name} - 成功 (${response.data ? response.data.length : 0} 条)`)
      if (response.data && response.data.length > 0) {
        console.log(`   数据结构:`, Object.keys(response.data[0]))
        console.log(`   示例数据:`, response.data[0])
      }
      return {
        success: true,
        data: response.data,
        count: response.data ? response.data.length : 0
      }
    } else {
      console.log(`❌ ${name} - 失败: ${response.message}`)
      return {
        success: false,
        message: response.message
      }
    }
  } catch (error) {
    console.log(`❌ ${name} - 异常: ${error.message}`)
    return {
      success: false,
      error: error.message
    }
  }
}

// 构建数据字典映射
export function buildDataDictionaryMaps(results) {
  const maps = {
    supplierLevelMap: new Map(),
    supplierCategoryMap: new Map(),
    businessNatureMap: new Map(),
    industryMap: new Map(),
    locationMap: new Map(),
    personMap: new Map()
  }

  // 供应商等级映射
  if (results.levels && results.levels.success && results.levels.data) {
    results.levels.data.forEach(item => {
      maps.supplierLevelMap.set(item.id, item.name)
    })
  }

  // 供应商类别映射
  if (results.categories && results.categories.success && results.categories.data) {
    results.categories.data.forEach(item => {
      maps.supplierCategoryMap.set(item.id, item.name)
    })
  }

  // 业务性质映射
  if (results.natures && results.natures.success && results.natures.data) {
    results.natures.data.forEach(item => {
      maps.businessNatureMap.set(item.id, item.name)
    })
  }

  // 行业映射
  if (results.industries && results.industries.success && results.industries.data) {
    results.industries.data.forEach(item => {
      maps.industryMap.set(item.id, item.name)
    })
  }

  // 地区映射
  if (results.locations && results.locations.success && results.locations.data) {
    results.locations.data.forEach(item => {
      maps.locationMap.set(item.id, item.name)
    })
  }

  // 人员映射
  if (results.persons && results.persons.success && results.persons.data) {
    results.persons.data.forEach(item => {
      maps.personMap.set(item.id, item.personName || item.name || item.userName)
    })
  }

  return maps
}

// 测试字段映射
export function testFieldMapping(supplier, maps) {
  const mapping = {
    supplierLevelName: maps.supplierLevelMap.get(supplier.supplierLevelId) || supplier.supplierLevelName || '未设置',
    supplierCategoryName: maps.supplierCategoryMap.get(supplier.supplierCategoryId) || supplier.supplierCategoryName || '未设置',
    businessNatureName: maps.businessNatureMap.get(supplier.businessNatureId) || supplier.businessNatureName || '未设置',
    industryName: maps.industryMap.get(supplier.industryId) || supplier.industryName || '未设置',
    locationName: maps.locationMap.get(supplier.locationId) || supplier.locationName || '未设置',
    purchaseRepresentativeName: maps.personMap.get(supplier.purchaseRepresentativeId) || supplier.purchaseRepresentativeName || '未指定'
  }

  console.log(`🔍 供应商 ${supplier.supplierName} 字段映射:`, {
    supplierLevelId: supplier.supplierLevelId,
    supplierLevelName: mapping.supplierLevelName,
    supplierCategoryId: supplier.supplierCategoryId,
    supplierCategoryName: mapping.supplierCategoryName,
    businessNatureId: supplier.businessNatureId,
    businessNatureName: mapping.businessNatureName,
    industryId: supplier.industryId,
    industryName: mapping.industryName,
    locationId: supplier.locationId,
    locationName: mapping.locationName,
    purchaseRepresentativeId: supplier.purchaseRepresentativeId,
    purchaseRepresentativeName: mapping.purchaseRepresentativeName
  })

  return mapping
}

// 导出测试函数
export default {
  testAllDataDictionaries,
  testDataDictionary,
  buildDataDictionaryMaps,
  testFieldMapping
} 